#!/bin/bash

red='\033[0;31m'
plain='\033[0m'
 while getopts 's:t:m' OPT; do
     case $OPT in
         s) source="$OPTARG";;
         t) target="$OPTARG";;
         m) func;;
         ?) func;;
     esac
 done
echo "源${source},复制${target}"
function checkSystem()
{
    result=$(id | awk '{print $1}')
    if [ $result != "uid=0(root)" ]; then
        echo "请以root身份执行该脚本"
        exit 1
    fi

    if [ ! -f /etc/lsb-release ];then
        echo "系统不是Ubuntu"
        exit 1
    fi
}

function copySite() {
     if [ ! -d "/var/www/${source}" ];then
          echo "源网站不存在"
          exit 1
     fi
     echo "复制源网站...."
     rm -rf /var/www/${target}
     cp -r /var/www/${source} /var/www/${target}
     chown -R www-data:www-data /var/www/${target}
}
function copyDatabase() {
  echo "复制数据库";
  dbkey=`echo "${target}" | sed -e "s/\./_/g"`
  dbname=${dbkey}
  dbuser=${dbkey}
  # dbpass=`cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 16 | head -n 1`
  dbpass='admin@qwe123'
  mysql -u root <<EOF
    DROP database IF EXISTS $dbname;
    DROP user IF EXISTS ${dbuser};
    CREATE DATABASE IF NOT EXISTS $dbname default charset utf8mb4;
    CREATE USER ${dbuser}@'%' IDENTIFIED BY '${dbpass}';
    GRANT ALL PRIVILEGES ON ${dbname}.* to ${dbuser}@'%';
    FLUSH PRIVILEGES;
EOF
   echo "备份源数据库"
   source_dbname=`echo "${source}" | sed -e "s/\./_/g"`
   mysqldump -uroot -padmin@qwe123 ${source_dbname} > ~/${source_dbname}.sql
   sed -i s/${source}/${target}/g ~/${source_dbname}.sql
   mysql -uroot -padmin@qwe123 ${dbname} < ~/${source_dbname}.sql
   mysql -uroot << EOF
    use ${dbname};
    DELETE FROM wpposts WHERE post_type IN ('product','product_variation');
    DELETE FROM wpterms WHERE term_id IN ( SELECT term_id FROM wpterm_taxonomy WHERE taxonomy='product_cat') AND slug<>'uncategorized';
    DELETE FROM wpterm_taxonomy WHERE taxonomy='product_cat';
EOF
   cd /var/www/${target}
   rm -rf /var/www/${target}/wp-config.php
   wp config create --dbname=$dbname --dbuser=$dbuser --dbpass=$dbpass --dbhost=127.0.0.1 --dbprefix=wp --allow-root
   wp config set ALLOW_UNFILTERED_UPLOADS true --raw --allow-root
   chown -R www-data:www-data /var/www/${target}/wp-config.php
}
function configNginx()
{
    cat > /etc/nginx/sites-available/${target}<<-EOF
server {
        server_name $target;

        root /var/www/$target;
        index index.html index.htm index.php;
        client_max_body_size 512m;

        location / {
          try_files \$uri \$uri/ @rewrites;
        }
        location @rewrites {
          rewrite ^(.+)$ /index.php last;
        }

        location ~ \.php$ {
          include snippets/fastcgi-php.conf;
          fastcgi_param REMOTE_ADDR \$proxy_protocol_addr;
          fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        }
    listen 80;
}
EOF
    rm -rf /etc/nginx/sites-enabled/${target}
    ln -s /etc/nginx/sites-available/${target} /etc/nginx/sites-enabled/${target}
    echo "WordPress Core SSL"
    apt install certbot python3-certbot-nginx -y
    certbot -n -m xiezhouyan@gmail.com -d $target --agree-tos  --nginx
#    sed -i "s/listen 80;/listen 980 proxy_protocol;/" /etc/nginx/sites-enabled/${target}
#    sed -i "s/listen 443 ssl;/listen 9443 proxy_protocol;/" /etc/nginx/sites-enabled/${target}
    systemctl restart php7.4-fpm mysql nginx
}

function output()
{
    echo "WordPress安装成功！"
    echo "==============================="
    echo -e "WordPress安装路径：${red}/var/www/${target}${plain}"
    echo -e "WordPress数据库：${red}${dbname}${plain}"
    echo -e "WordPress数据库用户名：${red}${dbuser}${plain}"
    echo -e "WordPress数据库密码：${red}${dbpass}${plain}"
    echo -e "WordPress管理员邮箱：${red}wp_admin@163.com"
    echo -e "WordPress管理员密码：${red}admin@qwe!123"
    echo -e "博客访问地址：${red}http://${target}${plain}"
    echo "==============================="
}

function main()
{
    copySite
    copyDatabase
    configNginx
    output
}

main